package dao;

import dataBase.memberData;

import java.util.ArrayList;
import java.util.Objects;

public class memberDao {

    public ArrayList<memberData> memberList = new ArrayList<>();

    public memberDao(){
        memberData member1 = new memberData();
        member1.setMemberId(0);
        member1.setMemberName("kjy");
        member1.setMemberMoney(10000.0f);
        memberList.add(member1);
    }

    public void register(String name, int id) {
        memberData member = new memberData();
        System.out.println("请输入注册姓名:");
        member.setMemberName(name);
        System.out.println("请输入卡号:");
        member.setMemberId(id);
        memberList.add(member);
        System.out.println(STR."注册成功 卡号\{id}姓名\{name}");
    }

    public boolean haveMember(int id) {
        for (memberData member : memberList) {
            if (Objects.equals(id, member.getMemberId())) {
                System.out.println("-----------会员信息------------");
                System.out.println(STR."会员姓名:\{member.getMemberName()}");
                System.out.println(STR."会员账号:\{member.getMemberId()}");
                System.out.println(STR."会员余额:\{member.getMemberMoney()}");
                System.out.println("------------------------------");
                return false;
            }
        }
        return true;
    }

    public boolean haveMember(String name) {
        for (memberData member : memberList) {
            if (Objects.equals(name, member.getMemberName())) {
                System.out.println("-----------会员信息------------");
                System.out.println(STR."会员姓名:\{member.getMemberName()}");
                System.out.println(STR."会员账号:\{member.getMemberId()}");
                System.out.println(STR."会员余额:\{member.getMemberMoney()}");
                System.out.println("------------------------------");
                return false;
            }
        }
        return true;
    }

    public memberData getMember(int id) {
        for (memberData member : memberList) {
            if (Objects.equals(id, member.getMemberId())) {
                return member;
            }
        }
        return null;
    }

    public memberData getMember(String name) {
        for (memberData member : memberList) {
            if (Objects.equals(name, member.getMemberName())) {
                return member;
            }
        }
        return null;
    }

    public boolean charge(int id, float num) {
        for (memberData member : memberList) {
            if (Objects.equals(id, member.getMemberId())) {
                System.out.println("tips:每充值100送20");
                if (num > 100) {
                    num += (int)(num / 100) * 20;
                }
                member.setMemberMoney(num + member.getMemberMoney());
                System.out.println(STR."充值成功，余额\{member.getMemberMoney()}");
                return true;
            }
        }
        return false;
    }

    public boolean charge(String name, float num) {
        for (memberData member : memberList) {
            if (Objects.equals(name, member.getMemberName())) {
                System.out.println("tips:每充值100送20");
                if (num > 100) {
                    num += (int)(num / 100) * 20;
                }
                member.setMemberMoney(num + member.getMemberMoney());
                System.out.println(STR."充值成功，余额\{member.getMemberMoney()}");
                return true;
            }
        }
        return false;
    }

    public int getIntegral(int id) {
        for (memberData member : memberList) {
            if (Objects.equals(id, member.getMemberId())) {
                return member.getMemberIntegral();
            }
        }
        return 0;
    }

    public int getIntegral(String name) {
        for (memberData member : memberList) {
            if (Objects.equals(name, member.getMemberName())) {
                return member.getMemberIntegral();
            }
        }
        return 0;
    }

    public boolean setIntegral(int id, int integral) {
        for (memberData member : memberList) {
            if (Objects.equals(id, member.getMemberId())) {
                member.setMemberIntegral(integral);
                return true;
            }
        }
        return false;
    }

    public boolean deleteMember(int id) {
        for (memberData member : memberList) {
            if (Objects.equals(id, member.getMemberId())) {
                memberList.remove(member);
                return true;
            }
        }
        return false;
    }

}
